/**
 * Created by Geo on 16/1/27.
 */
(function($){

    var source   = $("#entry-template").html();
    var template = Handlebars.compile(source);
    var $pagination = $('<div></div>');
    $pagination.addClass("m-pagination").attr("id","page");
    $(".person-content").append($("#page"));
    $("#page").pagination({
        pageSize: 10,
        total: 0,
        pageBtnCount:6,
        showFirstLastBtn:false,
        prevBtnText:"<",
        nextBtnText:">"
    });
    $("#page").on("pageClicked", function (event, data) {
        render(data);
    });
    /*获取投资人列表*/
    var render = function(opts){
		//remember data
		if(!!window.sessionStorage.getItem("moneyJson")){
			var moneyJson = window.sessionStorage.getItem("moneyJson");
		}
        $.get("/lt/investor/list", _.extend({},opts),function(data){
            if(!data.result){
                _.each(data.data,function(elem,index){
                    if(!!_.findWhere(JSON.parse(window.sessionStorage.getItem("investorList")),{"id":elem.id})){
                        elem.icon = _.findWhere(JSON.parse(window.sessionStorage.getItem("investorList")),{"id":elem.id}).icon;
                    }
                });
                $(".person-list").html(template(data));
                $("#page").pagination('setPageSize', data.total);
            }
        });
    }
    /*获取投资人详情列表*/
    var renderImg = function(){
        $.get("/lt/investor/hotest",{},function(data){
            if(!data.result){
                if(!window.sessionStorage.getItem("investorList")){
                    window.sessionStorage.setItem("investorList",JSON.stringify(data.data));
                }
            }
        });
    }
    /*获取行业领域列表*/
    var renderIndustry = function(){
        $.get("/lt/industry/list",function(data){
            if(!data.result){
                $(".project-investment-industry").empty().append('<span class="project-selected">全部</span>');
                _.each(data.data,function(elem){
                    var $itm = $("<span></span>").text(elem.name).attr("data-id",elem.id);
                    $(".project-investment-industry").append($itm);
                });
            }
        });
    }
    /*获取城市列表*/
    var renderCity = function(){
        $.get("/lt/project/citylist",function(data){
            if(!data.result){
                $(".project-each-district").empty().append('<span class="project-selected">全部</span>');
                _.each(data.data,function(elem){
                    var $itm = $("<span></span>").text(elem.cityName).attr("data-id",elem.cityID);
                    $(".project-each-district").append($itm);
                });
            }
        });
    }

    renderImg();
    renderIndustry();
    renderCity();
    render();

	//搜索
	$(".project-submit").click(function(){
		var moneyJson = {
			key: $(".project-search-content input").val(),
			investorType: $(".project-status > .project-selected").attr("data-status"),
			investFieldId: $(".project-investment-industry > .project-selected").attr("data-id"),
			cityID: $(".project-each-district > .project-selected").attr("data-id"),
			pageIndex: 1,
			pageSize: 20
		};
		console.log(moneyJson);
		window.sessionStorage.setItem("moneyJson", moneyJson);
		render(moneyJson);
	});
})(jQuery);